import React, { Component } from "react";

import { Link, Route, Switch, Redirect } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是home页面</div>;
  }
}
class About extends Component {
  render() {
    return <div>这是about页面</div>;
  }
}

class Detail extends Component {
  render() {
    return <div>详情页面 - {this.props.match?.params?.id}</div>;
  }
}

class Detail2 extends Component {
  render() {
    console.log(this.props);
    // const {
    //   location: { search },
    // } = this.props;
    // console.log(search);
    // const obj = new URLSearchParams(search);
    // console.log(obj.get("a"));
    // console.log(obj.get("b"));

    // {a:3,b:4}  ->   "?a=3&b=4"
    // new URLSearchParams({a:3,b:4}).toString()
    return <div>详情2页面</div>;
  }
}

class App extends Component {
  render() {
    return (
      <>
        <h2>react-router-v5-不同的传参的方式</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于页</Link>
          </li>
          <li>
            {/* params传参 */}
            <Link to="/detail/234">详情页</Link>
          </li>
          <li>
            {/* query传参 */}
            {/* <Link to="/detail2?a=3&b=4">详情页2</Link> */}
            <Link
              to={{
                pathname: "/detail2",
                search: "?a=3&b=4",
                hash: "#abc",
                // state是一个在url上面看不到的传参方式
                // state可以写任意对象
                state: { x: 10, y: 20 },
              }}
            >
              详情页2
            </Link>
          </li>
        </ul>

        <Switch>
          <Redirect from="/" to="/home" exact></Redirect>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/detail/:id" component={Detail}></Route>
          {/* query传参在这里不需要改动 */}
          <Route path="/detail2" component={Detail2}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
